package visao;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import modelo.Aluno;
import modelo.Turma;
import controle.AlunoControle;
import controle.TurmaControle;

import javax.swing.JMenuItem;
import javax.swing.ImageIcon;

public class CadastroAluno extends JFrame {

	private JPanel contentPane;
	private JTextField tfNome;
	private JTextField tfTelefone;
	private JTextField tfEndereco;
	private JTextField tfCpf;
	private JTextField tfEmail;
	private JButton btnSalvar;
	private JButton btnVoltar;
	private AlunoControle controle;
	private Aluno aluno = new Aluno();
	private JTextField tfMatricula;
	private TurmaControle turmaControle;
	private JComboBox cb_Turma;
	private JMenuItem mntmDeslogar;
	private JMenuItem mntmAula_1;
	private JMenuItem mntmAluno;
	private JMenuItem mntmFrequncia;
	private JMenuItem mntmTurma_1;
	
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadastroAluno frame = new CadastroAluno();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public void carregarAluno(Aluno aluno) {
		this.aluno = aluno;
		tfNome.setText(aluno.getNome());
		tfTelefone.setText(String.valueOf(aluno.getTelefone()));
		tfEndereco.setText(aluno.getEndereco());
		tfCpf.setText(aluno.getCpf());
		tfEmail.setText(aluno.getEmail());
		tfMatricula.setText(String.valueOf(aluno.getMatricula()));
		
	}

	public CadastroAluno() {
		setResizable(false);
		controle = new AlunoControle();
		turmaControle = new TurmaControle();
		setTitle("Green Pass / Cadastro Aluno");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 420);
		setLocationRelativeTo(null);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnInterface = new JMenu("Interface");
		menuBar.add(mnInterface);
		
		JMenuItem mntmAlterarSenha = new JMenuItem("Alterar Senha");
		mntmAlterarSenha.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				RedefinirSenha rf =  new RedefinirSenha();
				rf.setVisible(true);
				dispose();
			}
		});
		mnInterface.add(mntmAlterarSenha);
		
		mntmDeslogar = new JMenuItem("Deslogar");
		mntmDeslogar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TelaLogin tl =  new TelaLogin();
				tl.setVisible(true);
				dispose();
			}
		});
		mnInterface.add(mntmDeslogar);
		
		JMenu mnAula = new JMenu("Cadastrar");
		menuBar.add(mnAula);
		
		JMenuItem mntmAula = new JMenuItem("Aula");
		mntmAula.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CadastroAula cadastraAula =  new CadastroAula();
				cadastraAula.setVisible(true);
				dispose();
			}
		});
		mnAula.add(mntmAula);
		
		JMenuItem mntmTurma = new JMenuItem("Turma");
		mntmTurma.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CadastroTurma cadastraTurma =  new CadastroTurma();
				cadastraTurma.setVisible(true);
				dispose();
			}
		});
		mnAula.add(mntmTurma);
		
		JMenu mnBuscar = new JMenu("Buscar");
		menuBar.add(mnBuscar);
		
		mntmAula_1 = new JMenuItem("Aula");
		mntmAula_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BuscasAula ba =  new BuscasAula();
				ba.setVisible(true);
				dispose();
			}
		});
		mnBuscar.add(mntmAula_1);
		
		mntmAluno = new JMenuItem("Aluno");
		mntmAluno.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BuscasAluno bal =  new BuscasAluno();
				bal.setVisible(true);
				dispose();
			}
		});
		mnBuscar.add(mntmAluno);
		
		mntmFrequncia = new JMenuItem("Frequ\u00EAncia");
		mntmFrequncia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BuscasFrequencia bf =  new BuscasFrequencia();
				bf.setVisible(true);
				dispose();
			}
		});
		mnBuscar.add(mntmFrequncia);
		
		mntmTurma_1 = new JMenuItem("Turma");
		mntmTurma_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BuscasTurma bt =  new BuscasTurma();
				bt.setVisible(true);
				dispose();
			}
		});
		mnBuscar.add(mntmTurma_1);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tfNome = new JTextField();
		tfNome.setBounds(144, 71, 264, 20);
		contentPane.add(tfNome);
		tfNome.setColumns(10);
		
		MaskFormatter maskTelefone = null;
		try {
			maskTelefone = new MaskFormatter("(##) ####-####");
		} catch (Exception e) {
			e.printStackTrace();
		}
		tfTelefone = new JFormattedTextField(maskTelefone);
		tfTelefone.setBounds(144, 103, 264, 20);
		contentPane.add(tfTelefone);
		tfTelefone.setColumns(10);
		
		tfEndereco = new JTextField();
		tfEndereco.setBounds(144, 131, 264, 20);
		contentPane.add(tfEndereco);
		tfEndereco.setColumns(10);
		
		MaskFormatter maskCpf = null;
		try {
			maskCpf = new MaskFormatter("###.###.###-##");
		} catch (Exception e) {
			e.printStackTrace();
		}
		tfCpf = new JFormattedTextField(maskCpf);
		tfCpf.setBounds(144, 162, 264, 20);
		contentPane.add(tfCpf);
		tfCpf.setColumns(10);
		
		tfEmail = new JTextField();
		tfEmail.setBounds(144, 226, 264, 20);
		contentPane.add(tfEmail);
		tfEmail.setColumns(10);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon("C:\\EclipseATVDS\\Imagens Green Pass\\Imagens Finais\\Bot\u00E3oSalvarTelaCadastroAluno.png"));
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				aluno.setNome(tfNome.getText());
				aluno.setTelefone(tfTelefone.getText());
				aluno.setEndereco(tfEndereco.getText());
				aluno.setCpf(tfCpf.getText());
				Turma turma = (Turma) cb_Turma.getSelectedItem();
				aluno.setTurma(turma);
				aluno.setEmail(tfEmail.getText());
				aluno.setMatricula(Integer.parseInt(tfMatricula.getText()));
				
				try {
					controle.salvar(aluno);
					JOptionPane.showMessageDialog(null, "Aluno cadastrado!");
					tfNome.setText("");
					tfTelefone.setText("");
					tfEndereco.setText("");
					tfCpf.setText("");
					tfEmail.setText("");
					tfMatricula.setText("");
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e.getMessage());
				}
			}
		});
		btnSalvar.setBounds(317, 322, 91, 23);
		contentPane.add(btnSalvar);
		
		btnVoltar = new JButton("Voltar");
		btnVoltar.setIcon(new ImageIcon("C:\\EclipseATVDS\\Imagens Green Pass\\Imagens Finais\\Bot\u00E3oVoltarTelaCadastroAluno.png"));
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TelaCadastros tc = new TelaCadastros();
				tc.setVisible(true);
				dispose();
			}
		});
		btnVoltar.setBounds(32, 322, 91, 23);
		contentPane.add(btnVoltar);
		
		tfMatricula = new JTextField();
		tfMatricula.setBounds(144, 257, 264, 20);
		contentPane.add(tfMatricula);
		tfMatricula.setColumns(10);
		
		cb_Turma = new JComboBox();
		cb_Turma.setBounds(144, 193, 264, 22);
		contentPane.add(cb_Turma);
		List<Turma> turmas = turmaControle.listar();
		for (Turma turma : turmas) {
			cb_Turma.addItem(turma);
		}
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setIcon(new ImageIcon("BackgroundTelaCadastroAluno.png"));
		lblNewLabel_2.setBounds(0, 0, 444, 371);
		contentPane.add(lblNewLabel_2);
	}
}
